package iblink.adapter;

import packutil.idgen.IIdGenerator;
import packutil.throttle.IThrottle;
import packutil.throttle.IThrottleFactory;
import packutil.throttle.IThrottle.ThrottleRatePeriod;

public class IBClientFactory implements IIBClientFactory {

	private static int DEFAULT_HISTORICAL_DATA_THROTTLE_RATE = 6;
	private static ThrottleRatePeriod DEFAULT_HISTORICAL_DATA_THROTTLE_RATE_PERIOD = ThrottleRatePeriod.Minute;

	private IThrottle commonHistoricalDataThrottle;
	private IIdGenerator clientIdGenerator;
	private final String host;
	private final int port;

	public IBClientFactory(IIdGenerator clientIdGenerator,
			IThrottleFactory throttleFactory, String host, int port) {
		this.clientIdGenerator = clientIdGenerator;
		this.commonHistoricalDataThrottle = throttleFactory.createThrottle(
				DEFAULT_HISTORICAL_DATA_THROTTLE_RATE,
				DEFAULT_HISTORICAL_DATA_THROTTLE_RATE_PERIOD);
		this.host = host;
		this.port = port;
	}

	public void setHistDataThrottleRate(int throttleRate,
			ThrottleRatePeriod ratePeriod) {
		commonHistoricalDataThrottle.resetRate(throttleRate, ratePeriod);
	}

	@Override
	public IBClient createIBClient() {
		int clientId = clientIdGenerator.nextIdAsInt();
		return new IBClient(new IBWrapper(), commonHistoricalDataThrottle,
				clientId, host, port);
	}

}
